package com.example.test.exception;

import com.alibaba.fastjson.JSONException;

import com.baomidou.kaptcha.exception.KaptchaException;
import com.baomidou.kaptcha.exception.KaptchaIncorrectException;
import com.baomidou.kaptcha.exception.KaptchaNotFoundException;
import com.baomidou.kaptcha.exception.KaptchaTimeoutException;

import com.example.test.agent.entity.BaseResult;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.ExpiredCredentialsException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

import java.sql.SQLException;

/**
 * 全局异常处理
 */
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 系统异常
     *
     * @param e
     * @return
     * @throws Exception
     */
//    @ExceptionHandler(value = {Exception.class})
    public BaseResult exception(Exception e) {
        return BaseResult.error(400,"系统异常" + e.toString());
}


    /**
     * SQL异常
     * @param e
     * @return
     */
    @ExceptionHandler(SQLException.class)
    public BaseResult handleSQLException(SQLException e) {
        return BaseResult.error(500, e.toString());
    }

    /**
     * 上传附件大小异常
     * @param e
     * @return
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public BaseResult handleMaxUploadSizeExceededException(MaxUploadSizeExceededException e) {
        return BaseResult.error(400, "文件大小超出10MB限制, 请压缩或降低文件质量!");
    }


    /**
     * com.alibaba.fastjson.JSONException
     * @param e
     * @return
     */
    @ExceptionHandler(JSONException.class)
    public BaseResult handleJSONException(JSONException e) {
        return BaseResult.error(403, e.toString());
    }


    /**
     * 禁止访问
     * @param e
     * @return
     */
    @ExceptionHandler(AuthorizationException.class)
    public BaseResult handleAuthorizationException(AuthorizationException e) {
        return BaseResult.error(405, e.toString());
    }

    /**
     * 无权限
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthorizedException.class)
    public BaseResult handleUnauthorizedException(UnauthorizedException e) {
        return BaseResult.error(405, e.toString());
    }

    /**
     * 过期凭证
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredCredentialsException.class)
    public BaseResult handleExpiredCredentialsException(ExpiredCredentialsException e) {
        return BaseResult.error(403, e.toString());
    }

    /**
     * 凭证异常
     * @param e
     * @return
     */
    @ExceptionHandler(AuthenticationException.class)
    public BaseResult handleAuthenticationException(AuthenticationException e) {
        return BaseResult.error(403, e.toString());
    }

    /**
     * 账户异常
     * @param e
     * @return
     */
    @ExceptionHandler(AccountException.class)
    public BaseResult handleAccountException(AccountException e) {
        return BaseResult.error(403, e.toString());
    }


    /**
     * shiro异常
     * @param e
     * @return
     */
    @ExceptionHandler(ShiroException.class)
    public BaseResult handleShiroException(ShiroException e) {
        return BaseResult.error(401, "认证异常");
    }


    /**
     * 未认证
     * @param e
     * @return
     */
    @ExceptionHandler(UnauthenticatedException.class)
    public BaseResult handleUnauthenticatedException(UnauthenticatedException e) {
        return BaseResult.error(401, "未认证");
    }


    @ExceptionHandler(value = KaptchaException.class)
    public BaseResult kaptchaExceptionHandler(KaptchaException kaptchaException) {
        if (kaptchaException instanceof KaptchaIncorrectException) {
            return BaseResult.error(403, "验证码不正确");
        } else if (kaptchaException instanceof KaptchaNotFoundException) {
            return BaseResult.error(403, "验证码未找到");
        } else if (kaptchaException instanceof KaptchaTimeoutException) {
            return BaseResult.error(403, "验证码过期");
        } else {
            return BaseResult.error(403, "验证码渲染失败");
        }

    }

}
